﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Common;
using Models;
using Senparc.Weixin;
using Senparc.Weixin.Helpers.Extensions;
using Senparc.Weixin.Work.AdvancedAPIs;
using Senparc.Weixin.Work.CommonAPIs;
using QYAccessTokenContainer = Senparc.Weixin.Work.Containers.AccessTokenContainer;

namespace Web.Areas.Work.Controllers
{
    /// <summary>
    /// 部门管理
    /// </summary>
    public class DepartmentController : Controller
    {
        //
        // GET: /Work/Department/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 添加部门信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()
        {
            var department = new Department()
            {
                name = "部门" + CodeHelper.GetCode(),
                pareid = 1,
                order = CodeHelper.GetNum()
            };

            var accesstoken = QYAccessTokenContainer.TryGetToken(
                WxConfig.CorpID, 
                WxConfig.SecretContact);

            var ret = MailListApi.CreateDepartment(
                accesstoken, 
                department.name, 
                department.pareid, 
                department.order,
                department.id);

            // 返回值判断：
            // 成功：ret.errcode == ReturnCode_Work.请求成功

            return Content(department.ToJson());

        }

        /// <summary>
        /// 获取部门列表
        /// </summary>
        /// <returns></returns>
        public ActionResult GetList()
        {
            var accesstoken = QYAccessTokenContainer.TryGetToken(
                WxConfig.CorpID,
                WxConfig.SecretContact);

            var ret = MailListApi.GetDepartmentList(accesstoken);

            return Content(ret.ToJson());

        }

        /// <summary>
        /// 删除部门信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Delete()
        {
            var accesstoken = QYAccessTokenContainer.TryGetToken(
                WxConfig.CorpID,
                WxConfig.SecretContact);

            var ret = MailListApi.DeleteDepartment(accesstoken, 1802090031);

            return Content(ret.ToJson());

        }

        /// <summary>
        /// 编辑部门信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Update()
        {
            var accesstoken = QYAccessTokenContainer.TryGetToken(
                WxConfig.CorpID,
                WxConfig.SecretContact);

            var department = new Department()
            {
                id = 1802090034,
                name = "（改）部门" + CodeHelper.GetCode(),
                pareid = 1,
                order = CodeHelper.GetNum()
            };

            var ret = MailListApi.UpdateDepartment(
                accesstoken, 
                department.id,
                department.name,
                department.pareid,
                department.order);

            return Content(ret.ToJson());
        }


    }
}
